#!/usr/bin/perl

use SOAP::Transport::HTTP; 
use LWP::Simple;
use HTML::TreeBuilder::XPath;
use CGI qw(param); 

SOAP::Transport::HTTP::CGI->dispatch_to('servicio')->handle; 

package servicio; 

sub generar_web {
  my $centro = shift;

  my $primera = <<END;
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>AAP</title>
    </head>
    <body>
      <div>
END
 
  my $ultima = <<END;
      </div>
    </body>
  </html>
END

  return $primera.$centro.$ultima;
}

sub index {
  my $url = 'http://www.elpais.com/rss/index.html';
  my $web = LWP::Simple::get($url);   

  my $html = HTML::TreeBuilder::XPath->new();
  $html->parse($web);

  my $titulares_multimedia = $html->findnodes_as_string('/html/body/div[@class="estructura_2col estirar"]/div[@class="estructura_2col_1zq"]/
  div[@class="modContainer estirar"]/div[@class="estirar"]//a');

  my $secciones = $html->findnodes_as_string('/html/body/div[@class="estructura_2col estirar"]/div[@class="estructura_2col_1zq"]/
  div[@class="modContainer estirar"]/div[@class="mod size2 igualado"]//a');

  my $otras_categorias = $html->findnodes_as_string('/html/body/div[@class="estructura_2col estirar"]/div[@class="estructura_2col_1zq"]/
  div[@class="modContainer estirar"]/div[@class="mod size1 igualado"]//a');

  #se forman los links
  $links = "          ".$titulares_multimedia.$secciones.$otras_categorias;

  $links =~ s/href=\"(.*)\" id=\".*\"/href=\"http:\/\/www.elpais.com\/rss\/$1"/g;
  $links =~ s/\n/<br\/>\n          /g;
  $links =~ s/<strong>|<\/strong>//g;
  
  return generar_web($links);
}
